package com.lin.cms.service.impl;

import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lin.cms.util.Paging;
import com.lin.cms.criteria.ModelCriteria;
import com.lin.cms.domain.Model;
import com.lin.cms.service.ModelService;
import com.lin.cms.dao.ModelMapper;

@Service
public class ModelServiceImpl implements ModelService{
	
	public Model selectByPrimaryKey(Integer modelId){
		return modelMapper.selectByPrimaryKey(modelId);
	}
	
	public List<Model> selectByCriteria(ModelCriteria criteria){
		return modelMapper.selectByCriteria(criteria);
	}
	
	public int deleteByPrimaryKey(Integer modelId){
		int actualRowsAffected = 0;
		
		Model _oldModel = modelMapper.selectByPrimaryKey(modelId);
		
		if (_oldModel != null) {
			actualRowsAffected = modelMapper.deleteByPrimaryKey(modelId);
		}
		
		return actualRowsAffected;
	}
	
	public int deleteByCriteria(ModelCriteria criteria){
		int actualRowsAffected = 0;
		
		List<Model> _oldModel = modelMapper.selectByCriteria(criteria);
		
		if (_oldModel.size() != 0) {
			actualRowsAffected = modelMapper.deleteByCriteria(criteria);
		}
		
		return actualRowsAffected;
	}
	
	public int insert(Model model){
		return modelMapper.insert(model);
	}
	
	public int updateByPrimaryKeySelective(Model model){
		int actualRowsAffected = 0;
		
		int advertisingId = model.getModelId();

		Model _oldModel = modelMapper.selectByPrimaryKey(advertisingId);
		
		if (_oldModel != null) {
			actualRowsAffected = modelMapper.updateByPrimaryKeySelective(model);
		}
		
		return actualRowsAffected;
	}
	
	public int updateByPrimaryKey(Model model){
		int actualRowsAffected = 0;
		
		int advertisingId = model.getModelId();

		Model _oldModel = modelMapper.selectByPrimaryKey(advertisingId);
		
		if (_oldModel != null) {
			actualRowsAffected = modelMapper.updateByPrimaryKey(model);
		}
		
		return actualRowsAffected;
	}
	
	public List<Model> selectByPage(ModelCriteria criteria) {
		int totalResults = modelMapper.countByCriteria(criteria);
		Paging paging = criteria.getPaging();
		paging.setTotalResults(totalResults);
		
		return modelMapper.selectByPage(criteria,new RowBounds(paging.getFirstResult(),paging.getMaxResults()));
	}
	
	public int countByCriteria(ModelCriteria criteria){
		return modelMapper.countByCriteria(criteria);
	}
	
	public List<Model> getList(boolean containDisabled) {
		// TODO Auto-generated method stub
		return modelMapper.getList(containDisabled);
	}
	
	@Autowired
    private ModelMapper modelMapper;

	
}
